
let sdk = window['sdk'] = window['sdk'] ? window['sdk'] : {}
const ChannelName = {
    DEV: 'dev',
    WX: 'wx',
    QQ: 'qq',
    TT: 'tt',
    KS: 'ks',
    VIVO: 'vivo',
    OPPO: 'oppo',
    SWAN: 'swan',//百度
    F399: 'h5api',
    COCOS: 'AdSDK',
    NATIVE: 'SDKNative',
}
sdk['ChannelName'] = ChannelName
let SDKState = {
    loadFail: 0,//加载失败
    loadSucess: 1,//加载成功
    close: 2,//关闭状态
    loading: 3,//加载状态
    open: 4,//播放状态
    start: 5,//结束
    stop: 6

}
sdk['SDKState'] = SDKState
let SDKDir = {
    BOTTOM_MID: 0,//屏幕下中，常用
    BOTTOM_LEFT: 1,//屏幕下左
    TOP_MID: 2,//屏幕上中
    RIGHT_MID: 3,//屏幕右中
    LEFT_TOP: 4,//屏幕左上
    UP_MID: 5,//屏幕上中
    MID: 6,//屏幕中间
    XY: 7,//根据给定屏幕百分比设置广告位置
    NONE: 8,
    WHITE: 9,//白点，宽高为1的banner

}

sdk['SDKDir'] = SDKDir
let ResultState = {
    YES: 0,
    NO: 1,
    PROGRESS: 2,
}

sdk['ResultState'] = ResultState
const ADName = {
    RewaredAd: 'RewaredAd',
    BoxBannerAd: 'BoxBannerAd',
    BannerAd: 'BannerAd',
    BoxPortalAd: 'BoxPortalAd',
    InsertAd: 'InsertAd',
    CustomAd: 'CustomAd',
}

sdk['ADName'] = ADName

const CustomAdType = {
    H_PIC: 0,//横版纯图
    L_PIC_R_ARTICLE: 1,//左图右文
    L_ARTICLE_R_PIC: 2,//左文右图
    T_PIC_B_ARTICLE: 3,//上图下文
    T_ARTICLE_B_PIC: 4,//上文下图
    V_PIC: 5,//竖版纯图
    T_PIC_B_ARTICLE_B: 6,
    T_ARTICLE_B_PIC_B: 7,
    L_PIC_R_ARTICLE_B: 8,
}
sdk['CustomAdType'] = CustomAdType
function init(config, channelName) {
    //平台名称
    let cName = getChannelName(channelName);
    let sdkname = cName + '_sdk'
    let cls = window[sdkname];
    if (cls) {
        let channel = new cls
        console.log(' sdk ', channel)
        channel.init(window[getPlatName()], config[cName])
        sdk['channel'] = channel;
    }

}
sdk['init'] = init

function getVisibleSize() {
    return cc.view.getVisibleSize();
}
sdk['getVisibleSize'] = getVisibleSize
function getClientScaleX() {
    return 1;
}
sdk['getClientScaleX'] = getClientScaleX
function getClientScaleY() {
    return 1;
}
sdk['getClientScaleY'] = getClientScaleY

function getTransformStyle(node) {
    let transform = node.getComponent(cc.UITransformComponent);
    let width = transform.width;
    let height = transform.height;
    let position = node.worldPosition;
    let size = getVisibleSize();
    let x = position.x - transform.anchorX * width;
    let y = position.y - transform.anchorY * height + height;
    let style = { left: x, top: size.height - y, width: width, height: height };
    return style;
}
sdk['getTransformStyle'] = getTransformStyle

function getRealStyle(style, winSize) {
    let visibleSize = getVisibleSize()
    let rw = winSize.screenWidth / (visibleSize.width);
    let rh = winSize.windowHeight / (visibleSize.height);
    console.log('getRealStyle visibleSize ', visibleSize)
    console.log('getRealStyle winSize ', winSize)
    let width = style.width * rw;
    let height = style.height * rh;
    let left = style.left * rw;
    let top = style.top * rh;
    return {
        left: left,
        top: top,
        width: width,
        height: height,
    }
}

sdk['getRealStyle'] = getRealStyle

function getResizeStyle(style, size) {
    let rw = (style.width - size.width) / 2;
    let rh = (style.height - size.height) / 2;
    let temp = {
        left: style.left += rw,
        top: style.top += rh,
        width: size.width,
        height: size.height
    }
    return temp;
}
sdk['getResizeStyle'] = getResizeStyle

function getStyleByDir(dir, width, height, winSize) {
    let x = 0;
    let y = 0;
    switch (dir) {
        case sdk.SDKDir.BOTTOM_MID:
            x = (winSize.windowWidth - width) / 2
            y = winSize.windowHeight - height;
            break;
        case sdk.SDKDir.UP_MID:
            x = (winSize.windowWidth - width) / 2
            y = 0
            break;
        case sdk.SDKDir.MID:
            x = (winSize.windowWidth - width) / 2
            y = (winSize.windowHeight - height) / 2;
            break;
        case sdk.SDKDir.RIGHT_MID:
            x = (winSize.windowWidth - width)
            y = (winSize.windowHeight - height) / 2;
            break;
        case sdk.SDKDir.LEFT_TOP:
            x = 0
            y = 0
            break;
        case sdk.SDKDir.BOTTOM_LEFT:
            x = 0
            y = winSize.windowHeight - height;
            break;
        case sdk.SDKDir.XY:
            x = x * winSize.windowWidth;
            y = y * winSize.windowHeight;
            break;
        case sdk.SDKDir.WHITE:
            x = width + 1
            y = -height + 1
            break;
    }
    // console.log(' x222 ', x, 'y', y, 'width', width, 'height', height)
    return {
        left: x,
        top: y,
        width: width,
        height: height,
    }
}

sdk['getStyleByDir'] = getStyleByDir